package cn.rongdao.jrkabao.ui.setting;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.http.request.event.CompulsoryDownLineEvent;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.dialog.FRDialog;
import com.mifengkong.frwidget.dialog.FRDialogClickListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import cn.rongdao.jrkabao.BuildConfig;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.setting.VersionBean;
import cn.rongdao.jrkabao.dao.VersionDao;
import cn.rongdao.jrkabao.model.VersionModel;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.SelectCardUIComponent;
import cn.rongdao.jrkabao.ui.login.ModifyPasswordActivity;
import cn.rongdao.jrkabao.util.LoginUtil;


/**
 * Created by jiangyongxing on 2017/5/18.
 * 描述：设置界面
 */

public class SettingActivity extends BaseAutoFinishActivity {

    @BindView(R.id.as_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.as_modify_pwd)
    SelectCardUIComponent mModifyPwd;
    @BindView(R.id.as_abouts_as)
    SelectCardUIComponent mAboutUs;
    @BindView(R.id.as_current_version)
    SelectCardUIComponent mCurrentVersion;
    @BindView(R.id.as_custom_service)
    SelectCardUIComponent mCustomService;
    @BindView(R.id.as_tv_logout)
    TextView tvLogout;

    private boolean isHaveNewVersion;

    public static void start(Context context) {
        FRStartActivity.start(context, SettingActivity.class);
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {

        mAboutUs.setAboutUsContent();
        mCustomService.setCustomServiceContent();
        mCurrentVersion.setVersionInfoTitle();
        mModifyPwd.setModifyPwd();
        tvLogout.setVisibility(LoginUtil.isLogin() ? View.VISIBLE : View.GONE);

        initTitleBar();
        initListener();

        checkUpdate();
    }

    private void initListener() {
        mAboutUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AboutUsActivity.start(thisActivity());
            }
        });
        tvLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLogoutDialog();
            }
        });
        mCustomService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLoanConsultDialog();
            }
        });
        mCurrentVersion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHaveNewVersion) {
                    showDialogCheckUpdatePresenter();
                } else {
                    FRToast.showToastSafe("当前已是最新版");
                }
            }
        });
        mModifyPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModifyPasswordActivity.start(thisActivity());
            }
        });
    }

    private void checkUpdate() {
        VersionModel.getInstance().execute(new FRBaseObserver<VersionBean>(this, "请稍候...") {
            @Override
            protected void onBaseNext(VersionBean data) {
                if (!FRString.isEmpty(data.getNew_version()) && !data.getNew_version().equals(BuildConfig.VERSION_NAME)) {
                    VersionModel.getInstance().setVersionData(data);
                    isHaveNewVersion = true;
                    mCurrentVersion.setVersionInfoContent(true);
                } else {
                    VersionDao.getInstance().deleteVersionInfo();
                    isHaveNewVersion = false;
                    mCurrentVersion.setVersionInfoContent(false);
                }
            }
        });
    }

    private void showLogoutDialog() {
        new FRDialog.MDBuilder(getActivity())
                .setMessage("确认退出登录吗？")
                .setNegativeContentAndListener("取消", null)
                .setNegativeTextColor(ContextCompat.getColor(thisActivity(), R.color.c333333))
                .setPositiveTextColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary))
                .setPositiveContentAndListener("确认", new FRDialogClickListener() {
                    @Override
                    public boolean onDialogClick(View view) {
                        LoginUtil.logout(thisActivity());
                        EventBus.getDefault().post(new CompulsoryDownLineEvent());
                        return true;
                    }
                })
                .show();
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("设置");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if (BuildConfig.DEBUG) {
            titleBarUIComponent.initRightTextWithColor("网络请求设置", R.color.white, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SelectEnvironmentActivity.start(thisActivity());
                }
            });
        }
    }
}